<template>
  <div class="login_body">
    <!-- 左面 -->
    <div class="bg" />
    <!-- 右面 -->
    <div class="box">
      <div class="title">智慧园区-登录</div>
      <!-- 登陆表单 3+2-->
      <el-form ref="loginRef" :model="loginForm" :rules="loginRules">
        <el-form-item
          label="账号"
          prop="username"
        >
          <el-input v-model.trim="loginForm.username" placeholder="请输入账号" />
        </el-form-item>

        <el-form-item
          label="密码"
          prop="password"
        >
          <el-input v-model.trim="loginForm.password" show-password placeholder="请输入密码" />
        </el-form-item>
        <el-form-item
          label="模型uuid:"
          prop=""
        >
          <el-input />
        </el-form-item>

        <el-form-item>
          <el-checkbox v-model="remember">记住我</el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" class="login_btn" @click="doLogin">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { FORM_KEY } from '@/constants/KEY'

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''

      },
      remember: true,
      loginRules: {
        username: [
          { required: true, message: '请输入账号！', trigger: ['blur', 'change'] },
          { min: 3, max: 8, message: '用户名格式不对哟', trigger: ['blur', 'change'] }
        ],
        password: [
          { required: true, message: '请输入密码！', trigger: ['blur', 'change'] }
          // { min: 6, max: 26, message: '用户密码格式不对哟', trigger: ['blur', 'change'] }
        ]
        // remember: [
        //   { validator: (rule, value, callback) => {
        //     // rule :规则
        //     // value:检查的数据
        //     // callback函数：执行这个函数
        //     // 成功执行callback 失败也执行callback(错误对象 new Error(错误信息))
        //     // value ? callback() : callback(localStorage.setItem('id'))
        //     value ? callback() : callback('false')
        //   } }
        // ]

      }
    }
  },
  created() {
    const loginData = localStorage.getItem(FORM_KEY)
    if (loginData) {
      const { username, password } = JSON.parse(loginData)
      this.loginForm.username = username
      this.loginForm.password = password
      // console.log(JSON.stringify(this.loginForm))
    }
  },
  methods: {
    doLogin() {
      this.$refs.loginRef.validate(async(valid) => {
        // console.log(valid)

        if (valid) {
          try {
            // TODO登录
            await this.$store.dispatch('user/doLogin', this.loginForm)

            // 判断用户是否点击记住我
            if (this.remember) {
              localStorage.setItem(FORM_KEY, JSON.stringify(this.loginForm))
            } else { localStorage.removeItem(FORM_KEY) }
            // 拿到退出登录前路径
            if (this.$route.query.redirect) {
              // console.log(this.$route.query.redirect)
              this.$router.replace(this.$route.query.redirect)
            } else {
              this.$router.replace('/')
            }
            this.$message.success('登陆成功')
            // console.log(this.$route.query.redirect)
          } catch (error) {
            return
          }
        }
      })
    }

  }
}

</script>

<style scoped lang="scss" >
  .login_body {
    display: flex;
  }
  .bg {
    width: 60vw;
    height: 100vh;
    background: url('~@/assets/login-bg.svg') no-repeat;
    background-position: right top;
    background-size: cover;
  }
  .box {
    margin: 200px 10% 0;
    flex: 1;
    .title {
      padding-bottom: 76px;
      font-size: 26px;
      font-weight: 500;
      color: #1e2023;
    }
    ::v-deep() {
      .ant-form-item {
        display: flex;
        margin-bottom: 62px;
        flex-direction: column;
      }
      .ant-form-item-label label {
        font-size: 16px;
        color: #8b929d;
      }
      .ant-input,
      .ant-input-password {
        border-radius: 8px;
      }
    }
  }
  .login_btn{
    width: 100%;
  }
</style>
